/*
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "./garmindumpreader.h"


GarminDumpReader::GarminDumpReader(QString filename)
{  
  if ( (data = garmin_load(filename.toStdString().c_str())) != NULL ) {
    //garmin_print_data(data,stdout,0);
    //garmin_free_data(data);
  }
  else
    qWarning("GarminDumpReader: no data read");
}

/**
@return pointer to garmin_data
*/
garmin_data *GarminDumpReader::get()
{
  return data;
}

void GarminDumpReader::explore(garmin_data* d, int spaces)
{
  printSpaces(spaces);
  if (d->type == data_Dnil)
  {
    printf("datatype: nil\n");
  }
  else if (d->type == data_Dlist)
  {
    printf("datatype: list\n");
    
    garmin_list_node * n;
    for ( n = ((garmin_list*)d->data)->head; n != NULL; n = n->next ) {
      explore(n->data, spaces + 2);
    }
  }
  else
  {
    printf("datatype: %d\n", d->type);
  }
}

void GarminDumpReader::printSpaces(int spaces)
{
  int i;
  for (i = 0; i < spaces; i++)
    printf(" ");
}

void GarminDumpReader::describe()
{
  explore(data, 0);
}
